package com.yuandian.management.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuandian.api.management.entity.ConfResource;
import com.yuandian.api.management.validate.InsertFlag;
import com.yuandian.api.management.validate.UpdateFlag;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.bpm.common.log.annotation.SysLog;
import com.yuandian.management.service.ConfResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 服务资源表-Controller
 * @author zfsn
 * @date 2023-3-22
 */
@Api(tags = "业务服务资源管理")
@RestController
@RequestMapping("/resource")
public class ConfResourceController {

	@Autowired
	private ConfResourceService confResourceService;

	/**
	 * 通过主键查询数据
	 * @param idList 主键集合
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键查询数据", notes = "通过主键查询数据，支持批量查询")
	@GetMapping("/{ids}")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "id(可多选)", paramType = "query", required = true, dataTypeClass = Integer.class)
	})
	public R<List<ConfResource>> queryByIds(@PathVariable("ids") List<Integer> idList) {
		return R.ok(confResourceService.listByIds(idList));
	}

	/**
	 * 分页查询
	 * @param confResource 筛选条件
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@PostMapping
	@ApiImplicitParams({
			@ApiImplicitParam(name = "type", value = "资源类型，0：非首端服务资源(默认)、1：关键加载项服务资源、2:步骤服务资源", paramType = "body", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "isConfirm", value = "是否确认，0：否(默认)、1：是", paramType = "body", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "identifyType", value = "服务资源类型: 1：url、2：api、3：sql", paramType = "body", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "identifyStr", value = "识别串", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "parentResourceCode", value = "父服务资源编码", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "protocolCode", value = "协议资源编码", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "resourceCode", value = "服务资源编码", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "pageSize", value = "每页大小", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "pageNum", value = "当前页数", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "keyword", value = "关键字", paramType = "body", required = false, dataType = "String"),
	})
	public R<Page<ConfResource>> listPage(@RequestBody @ApiIgnore ConfResource confResource) {
		return R.ok(confResourceService.listPage(confResource));
	}

	/**
	 * 新增数据
	 * @param confResource 实例对象
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "新增数据", notes = "新增数据")
	@SysLog("新增服务资源表数据")
	@PostMapping("/save")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "type", value = "资源类型，0：非首端服务资源(默认)、1：关键加载项服务资源、2:步骤服务资源", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "isConfirm", value = "是否确认，0：否(默认)、1：是", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "identifyType", value = "服务资源类型: 1：url、2：api、3：sql", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "identifyStr", value = "识别串", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "parentResourceCode", value = "父服务资源编码", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "protocolCode", value = "协议编码  AR-AP-HTTP-00000306", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "protocolType", value = "协议类型 AR-AP-HTTP", paramType = "body", required = true, dataType = "String"),
	})
	public R<String> save(@Validated({InsertFlag.class}) @RequestBody @ApiIgnore ConfResource confResource) {
		return confResourceService.saveConfig(confResource);
	}

	/**
	 * 通过主键更新数据
	 * @param confResource 实例对象
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键更新数据", notes = "通过主键更新数据")
	@SysLog("更新服务资源表数据")
	@PutMapping
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "id", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "type", value = "资源类型，0：非首端服务资源(默认)、1：关键加载项服务资源、2:步骤服务资源", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "isConfirm", value = "是否确认，0：否(默认)、1：是", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "identifyType", value = "服务资源类型: 1：url、2：api、3：sql", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "identifyStr", value = "识别串", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "parentResourceCode", value = "父服务资源编码", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "protocolType", value = "协议类型  AR-AP-HTTP-00000306", paramType = "body", required = true, dataType = "String"),
	})
	public R<String> updateById(@Validated({UpdateFlag.class}) @RequestBody @ApiIgnore ConfResource confResource) {
		return confResourceService.updateDataById(confResource);
	}

	/**
	 * 通过主键删除数据
	 * @param ids 主键
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键删除数据", notes = "通过主键删除数据")
	@SysLog("删除服务资源表数据")
	@DeleteMapping("delete/{ids}")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "id(可多选)", paramType = "path", required = true, dataTypeClass = Integer.class)
	})
	public R<Boolean> removeByIds(@PathVariable("ids") List<Integer> ids){
		return R.ok(confResourceService.removeBatchByIds(ids));
	}


	/**
	 * 确认业务服务资源
	 * @param resourceCodes 主键
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "确认业务服务资源", notes = "确认业务服务资源")
	@GetMapping("confirmResource/{resourceCodes}")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "resourceCodes", value = "资源编码(可多选)", paramType = "path", required = true, dataTypeClass = String.class)
	})
	public R<String> confirmResource(@PathVariable("resourceCodes") List<String> resourceCodes){
		return confResourceService.confirmResource(resourceCodes, true);
	}

	/**
	 * 取消业务服务资源
	 * @param resourceCodes 主键
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "取消业务服务资源", notes = "取消业务服务资源")
	@GetMapping("cancelResource/{resourceCodes}")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "resourceCodes", value = "资源编码(可多选)", paramType = "path", required = true, dataType = "String")
	})
	public R<String> cancelResource(@PathVariable("resourceCodes") List<String> resourceCodes){
		return confResourceService.confirmResource(resourceCodes, false);
	}

	/**
	 * 获取业务服务资源
	 * @return 通用返回对象
	 */
	@ApiIgnore
	@GetMapping("/getBusinessResourceMap")
	public R<Map<String, ConfResource>> getBusinessResourceMap(){
		//只获取确认状态的服务资源
		List<ConfResource> confResources = confResourceService.list(new LambdaQueryWrapper<ConfResource>().eq(ConfResource::getStatus, 1));
		Map<String, ConfResource> resultMap = confResources.stream()
				.collect(Collectors.toMap(ConfResource::getResourceCode, Function.identity(), (key1, key2) -> key2));

		return R.ok(resultMap);
	}


	/**
	 * 根据系统id和业务编码查询业务服务资源
	 * @param systemId
	 * @param businessCode
	 * @return
	 */
	@ApiOperation(value = "根据系统id和业务编码查询业务服务资源", notes = "根据系统id和业务编码查询业务服务资源")
	@GetMapping("/{systemId}/{businessCode}")
	public R<Set<String>> getResourceBySystemIdAndBusinessCode(@PathVariable("systemId") Integer systemId, @PathVariable("businessCode") String businessCode){
		return R.ok(confResourceService.getResourceBySystemIdAndBusinessCode(systemId, businessCode));
	}


}